//
//  SEGChangePasswordViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/12.
//

#import "SEGChangePasswordViewModel.h"
#import "SEGRequestManager.h"

@interface SEGChangePasswordViewModel ()

@property (nonatomic, strong) SEGRequestManager *updatePwdManager;

@end

@implementation SEGChangePasswordViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createRequestConfigInfoSignal];
        [self seg_createRefreshImgSignal];
        [self seg_createSureBtnSignal];
    }
    return self;
}
- (void)seg_createRequestConfigInfoSignal {
    @weakify(self);
    self.reqConfigSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [self.updatePwdManager requestPasswordConfigInfo:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if ([result isKindOfClass:[NSString class]]) {
                [subscriber sendNext:result];
                [subscriber sendCompleted];
            }
        }];
        return nil;
    }];
}
- (void)seg_createRefreshImgSignal {
    @weakify(self);
    self.refreshImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        self.checkImgStr = @"";
        [self.updatePwdManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                if ([self.updatePwdManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                    NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.updatePwdManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
                    [subscriber sendNext:decodedImage];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];

        return nil;
    }];
}
- (void)seg_createSureBtnSignal {
    @weakify(self);
    self.sureBtnSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSString *strTel = [SEGLoginV2Manager segUserTelFromLocal];
        if (self.oldPwdStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入原密码"}];
            [subscriber sendError:error];
            return nil;
        }
        if (self.freshPwdStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入新密码"}];
            [subscriber sendError:error];
            return nil;
        }
        if (self.confirmPwdStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请确认新密码"}];
            [subscriber sendError:error];
            return nil;
        }
        if (self.checkImgStr.length == 0) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入验证码"}];
            [subscriber sendError:error];
            return nil;
        }
        if (self.freshPwdStr.length < 8) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"新密码至少为8个字符"}];
            [subscriber sendError:error];
            return nil;
        }
        if (![self.freshPwdStr isEqualToString:self.confirmPwdStr]) {
            NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"两次输入的新密码不一致"}];
            [subscriber sendError:error];
            return nil;
        }

        [self.updatePwdManager requestUserInfoUpdatePwdByTel:strTel newPwd:self.freshPwdStr oldPwd:self.oldPwdStr imgCode:self.checkImgStr randomToken:self.updatePwdManager.seg_segtelcaptchasbyregistermodel.randomToken finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (isSuccessed) {
                // 密码传空字符串
                [SEGLoginV2Manager segSaveUserPasswordToLocal:@""];
                NSDictionary *dic = @{@"message":errorMessage};
                [subscriber sendNext:dic];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}

#pragma mark - Getter
- (SEGRequestManager *)updatePwdManager {
    if (!_updatePwdManager) _updatePwdManager = [[SEGRequestManager alloc] init];
    return _updatePwdManager;
}

@end
